package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class MobileValidateCodeController {

    @Autowired
    private RedisTemplate redisTemplate;
    //请求 URL: http://localhost:17080/mobile/validateCode/send?type=ORDER&telephone=17630125761
    //请求方法: POST
    //发送验证
    @PostMapping("/send")
    public Result send(@RequestParam("type") String type,@RequestParam("telephone") String telephone){
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        System.out.println("验证码是:"+code);
        String key =  "_"+"ORDER"+"_"+ telephone;
        redisTemplate.opsForValue().set(key, code, 2, TimeUnit.MINUTES);
        return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

}
